﻿using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using SemiCASE.Business;

public partial class Projects : SemiCASEBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            JavascriptHelper.PopUpWindowEventiEkle(HyperlinkNewProject, "~/PopUp/CreateProject.aspx", 500, 250, true);
            LoadProjects();
        }
    }

    private void LoadProjects()
    {
        DataTable dt = new ProjectBS().LoadProjects();

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            dt.Rows[i]["SiraNo"] = (i + 1).ToString();
        }

        if (dt.Rows.Count > 0)
        {
            gvProjects.DataSource = dt;
            gvProjects.DataBind();
            LabelRowCount.Text = "Row Count: " + dt.Rows.Count.ToString();
            Session["ProjectList"] = dt;
        }
        else
        {
            gvProjects.DataSource = null;
            gvProjects.DataBind();
            LabelRowCount.Text = String.Empty;
        }
    }

    protected void gvProjects_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType != DataControlRowType.DataRow)
            return;

        string projectID = gvProjects.DataKeys[e.Row.RowIndex].Values["ProjectID"].ToString();
        string queryString = "?ProjectID=" + projectID;

        HyperLink hyperLinkGuncelle = (HyperLink)e.Row.FindControl("HyperLinkUpdate");
        JavascriptHelper.PopUpWindowEventiEkle(hyperLinkGuncelle, "~/PopUp/CreateProject.aspx" + queryString, 500, 250, true);

        HyperLink HyperLinkTestCase = (HyperLink)e.Row.FindControl("HyperLinkTestCase");
        JavascriptHelper.PopUpWindowEventiEkle(HyperLinkTestCase, "~/PopUp/ProjectTestCases.aspx" + queryString, 800, 500, true);

        HyperLink HyperLinkWorkItems = (HyperLink)e.Row.FindControl("HyperLinkWorkItems");
        JavascriptHelper.PopUpWindowEventiEkle(HyperLinkWorkItems, "~/PopUp/ProjectWorkItems.aspx" + queryString, 800, 500, true);
    }

    protected void gvProjects_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "DeleteProject")
        {
            DeleteProject(e.CommandArgument.ToString());
        }
        else if (e.CommandName == "UseCaseList")
        {
            NavigateUseCases(e.CommandArgument.ToString());
        }
    }

    private void NavigateUseCases(string projectID)
    {
        ProjectName(projectID);
        Response.Redirect("~/UseCases.aspx?ProjectID=" + projectID);
    }

    private void ProjectName(string projectID)
    {
        if (Session["ProjectList"] != null)
        {
            DataTable dt = (DataTable)Session["ProjectList"];
            foreach (DataRow row in dt.Rows)
            {
                if (row["ProjectID"].ToString() == projectID)
                {
                    Session["ProjectName"] = row["Name"].ToString();
                    break;
                }
            }
        }
    }

    private void DeleteProject(string projectID)
    {
        try
        {
            new ProjectBS().DeleteProject(new Guid(projectID));
            LoadProjects();
            MessageBox("Project deleted successfully", SemiCASE.Interfaces.MesajTuruEnum.Bilgi);
        }
        catch (Exception exc)
        {
            MessageBox("Project couldn't be deleted!", SemiCASE.Interfaces.MesajTuruEnum.Hata);
        }
    }

    protected void gvProjects_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvProjects.PageIndex = e.NewPageIndex;
        DataTable dt = (DataTable)Session["ProjectList"];
        gvProjects.DataSource = dt;
        gvProjects.DataBind();
    }
}